package com.example.manage_c20086.api;
import com.example.manage_c20086.cutil.cTool;
import com.pojo.util.Tool;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import com.pojo.Entity.Notice;
import com.pojo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("api/notice")
public class NoticeApi {
    @Autowired
    private RestTemplate restTemplate;
    private static final String REST_URL_PREFIX="http://NOTICE-MANAGEMENT/notice";
    private static final String IDENTIFYING="identifying";
    @GetMapping("/getlnotice")
    public Result<?> getlnotice(@RequestParam Integer page, @RequestParam Integer number){
        return  restTemplate.getForObject(REST_URL_PREFIX+"/getlnotice/"+page+"/"+number,Result.class);
    }
    @GetMapping("/getnotice")
    public Result<?>getnotice(@RequestParam Integer nID){
        return  restTemplate.getForObject(REST_URL_PREFIX+"/getnotice/"+nID,Result.class);
    }
    @PostMapping("/update")
    public Result<?>updatenotice( Notice notice,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(notice,map);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
          return  restTemplate.postForObject(REST_URL_PREFIX+"/update",request,Result.class);
    }
    @PostMapping("/insert")
    public Result<?>insertnotice( Notice notice, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(notice,map);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return  restTemplate.postForObject(REST_URL_PREFIX+"/insert",request,Result.class);
    }
    @GetMapping("/delete")
    public Result<?>deletenotice(@RequestParam  Integer nID,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("nID",nID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return  restTemplate.postForObject(REST_URL_PREFIX+"/delete",request,Result.class);
    }
}
